package cn.com.gift.controller;

import cn.com.gift.dto.GiftCompanyDTO;
import cn.com.gift.dto.LoginUserInfo;
import cn.com.gift.dto.ProjectListParam;
import cn.com.gift.mapper.CompanyMapper;
import cn.com.gift.pojo.GiftCompany;
import cn.com.gift.pojo.GiftHistory;
import cn.com.gift.pojo.GiftProject;
import cn.com.gift.service.GiftCompanyService;
import cn.com.gift.service.GiftHistoryService;
import cn.com.gift.service.GiftProjectService;
import cn.com.gift.utils.GlobalVariable;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2018-2024
 * FileName: ProjectInfoController
 * Author:   Mr
 * Date:     2024/6/4 20:33
 * Description:
 */
@RestController
@RequestMapping("/gift")
public class ProjectInfoController {
    @Autowired
    private GiftProjectService giftProjectService;

    @Autowired
    private GiftCompanyService giftCompanyService;

    @Autowired
    private GiftHistoryService giftHistoryService;

    @Autowired
    private CompanyMapper companyMapper;

    @GetMapping("/project/detail")
    public Map<String, Object> detail(@RequestParam("id") String id) {
        Map<String, Object> result = new HashMap<>();
        if (id == null) {
            result.put("code", 500);
            result.put("message","当前项目id不能为空");
            return result;
        }
        GiftProject giftProject = giftProjectService.lambdaQuery().eq(GiftProject::getId,id)
                .one();

        if (giftProject == null) {
            result.put("code", 500);
            result.put("message","当前项目不存在");
            return result;
        }
        result.put("project", giftProject);

        List<GiftCompanyDTO> list = companyMapper.selectGiftCompanyInfo(id);
        result.put("detail", list);

        result.put("code", 200);
        return result;
    }

    @PostMapping("/project/page")
    public IPage<GiftProject> listPage(@RequestBody ProjectListParam projectListParam) {
        LambdaQueryWrapper<GiftProject> wrapper = new LambdaQueryWrapper<>();
        String projectName = projectListParam.getProjectName();
        if (!StringUtils.isEmpty(projectName)) {
            wrapper.like(GiftProject::getProjectName, projectName);
        }
        wrapper.orderByDesc(GiftProject::getCreateTime);
        int size = giftProjectService.list(wrapper).size();

        Page<GiftProject> page = new Page<>
                (projectListParam.getCurrentPage() == null ? 1 : projectListParam.getCurrentPage(),
                        projectListParam.getSize() == null ? 10 : projectListParam.getSize(),size);
        return giftProjectService.page(page, wrapper);
    }

    @GetMapping("/project/byId")
    public Map<String, Object> queryOne(@RequestParam("id") String id) {

        Map<String, Object> result = new HashMap<>();
        if (id == null) {
            result.put("code", 500);
            result.put("message","当前账单id不能为空");
            return result;
        }

        GiftProject byId = giftProjectService.getById(id);
        if (byId == null ) {
            result.put("code", 500);
            result.put("message","当前项目不存在");
            return result;
        }
        result.put("data",byId);
        result.put("code", 200);
        return result;
    }

    @PostMapping("/project/delete")
    public Map<String, Object> delete(@RequestParam("id") String id) {
        Map<String, Object> result = new HashMap<>();
        if (id == null) {
            result.put("code", 500);
            result.put("message","当前项目id不能为空");
            return result;
        }
        giftProjectService.removeById(id);

        LambdaQueryWrapper<GiftHistory> giftWrapper = new LambdaQueryWrapper<>();
        giftWrapper.eq(GiftHistory::getProjectId, id);
        giftHistoryService.remove(giftWrapper);
        result.put("code", 200);
        return result;
    }

    @PostMapping("/project/add")
    public Map<String, Object> add(@RequestBody GiftProject giftProject) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(giftProject.getProjectName())) {
            result.put("code", 500);
            result.put("message","项目名称不允许为空");
            return result;
        }

        if (StringUtils.isEmpty(giftProject.getProjectCode())) {
            result.put("code", 500);
            result.put("message","项目编码不允许为空");
            return result;
        }
        if (!StringUtils.isEmpty(giftProject.getProjectDesc()) && giftProject.getProjectDesc().length() > 500) {
            result.put("code", 500);
            result.put("message","项目描述不能超过500个字符");
            return result;
        }

        GiftProject one = giftProjectService.lambdaQuery()
                .eq(GiftProject::getProjectName, giftProject.getProjectName())
                .one();

        if (StringUtils.isEmpty(giftProject.getId()) && one!=null) {
            result.put("code", 500);
            result.put("message","存在相同项目名称不允许新增");
            return result;
        }

        giftProjectService.saveOrUpdate(giftProject);
        result.put("code", 200);
        return result;
    }

    @PostMapping("/history/add")
    public Map<String, Object> historyAdd(@RequestBody GiftHistory giftHistory) {
        Map<String, Object> result = new HashMap<>();
        giftHistoryService.saveOrUpdate(giftHistory);
        result.put("code", 200);
        return result;
    }

    @PostMapping("/company/add")
    public Map<String, Object> companyAdd(@RequestBody GiftCompany giftCompany) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(giftCompany.getCompanyName())) {
            result.put("code", 500);
            result.put("message","企业名称不允许为空");
            return result;
        }

        if (!StringUtils.isEmpty(giftCompany.getCompanyAddr()) && giftCompany.getCompanyAddr().length() > 255) {
            result.put("code", 500);
            result.put("message","企业地址不能超过255个字符");
            return result;
        }

        GiftCompany one = giftCompanyService.lambdaQuery()
                .eq(GiftCompany::getCompanyName, giftCompany.getCompanyName())
                .one();

        if (StringUtils.isEmpty(giftCompany.getId()) && one!=null) {
            result.put("code", 500);
            result.put("message","存在相同企业名称不允许新增");
            return result;
        }
        giftCompanyService.saveOrUpdate(giftCompany);
        result.put("code", 200);
        return result;
    }


    @PostMapping("/list")
    public Map<String, Object> list() {
        Map<String, Object> result = new HashMap<>();
        List<GiftCompany> list = giftCompanyService.list();
        result.put("companyList", list);

        if (list.size() > 0) {
            result.put("row", Math.ceil(Math.sqrt(list.size())));
        }

        List<GiftProject> projects = giftProjectService.list();
        result.put("projectList", projects);
        result.put("code", 200);

        return result;
    }

    @PostMapping("/company/delete")
    public Map<String, Object> companyDelete(@RequestParam("id") String id) {
        Map<String, Object> result = new HashMap<>();
        if (id == null) {
            result.put("code", 500);
            result.put("message","当前企业id不能为空");
            return result;
        }
        giftCompanyService.removeById(id);
        result.put("code", 200);
        return result;
    }

    @PostMapping("/company/page")
    public IPage<GiftCompany> companyPage(@RequestBody ProjectListParam projectListParam) {
        LambdaQueryWrapper<GiftCompany> wrapper = new LambdaQueryWrapper<>();
        String companyName = projectListParam.getCompanyName();
        if (!StringUtils.isEmpty(companyName)) {
            wrapper.like(GiftCompany::getCompanyName, companyName);
        }
        wrapper.orderByDesc(GiftCompany::getCreateTime);
        int size = giftCompanyService.list(wrapper).size();

        Page<GiftCompany> page = new Page<>
                (projectListParam.getCurrentPage() == null ? 1 : projectListParam.getCurrentPage(),
                        projectListParam.getSize() == null ? 10 : projectListParam.getSize(),size);
        return giftCompanyService.page(page, wrapper);
    }

    @GetMapping("/company/byId")
    public Map<String, Object> companyById(@RequestParam("id") String id) {

        Map<String, Object> result = new HashMap<>();
        if (id == null) {
            result.put("code", 500);
            result.put("message","当前企业id不能为空");
            return result;
        }

        GiftCompany byId = giftCompanyService.getById(id);
        if (byId == null ) {
            result.put("code", 500);
            result.put("message","当前企业不存在");
            return result;
        }
        result.put("data",byId);
        result.put("code", 200);
        return result;
    }

}
